<?php

use Base\Exception\Controller as Exception;


/**
 * @name 消息、通知
 */
class Controller_Msg extends \Modules\Admin\Controllers\Controller {

    public function indexAction() {
    }


    /**
     * @name 通知列表
     */
    public function noticeAction() {

    }

    /**
     * @name 通知列表数据源
     */
    public function noticeListAction() {
        $list = (new \Data\Netease\Msg\MsgBody())->getList(array('type'=>\Dao\Db\Netease\MsgBody::TYPE_ALL),['ctime' => 'desc']);
        //解析消息体
        foreach ($list as &$item){
            $body = json_decode($item['body'],true);
            $item['body'] = json_encode($body,JSON_UNESCAPED_UNICODE);
        }
        $this->response['data'] = $list;
    }

    /**
     * @name 编辑通知
     */
    public function noticeEditAction() {
    }

    /**
     * @name 提交发送
     * @throws Exception
     */
    public function noticeSubmitAction() {
        $content = $this->getParams('content');
        $content = trim($content);  //取出前后空格
        $body = json_decode($content,true); //格式化成数组
        if(!$body['title'] || !$body['content']){  //title 和 content 为消息的必填字段
            throw new Exception('error.admin.msg_lose_content_error');
        }
        (new \Service\Netease\Msg())->allSend($body);
        Header("Location: notice");
    }
}
